import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hm_show_flutter/constant/type.dart';
import 'package:hm_show_flutter/entity/home_entity.dart';
import 'package:hm_show_flutter/utils/navigator_util.dart';
import 'package:hm_show_flutter/utils/shared_preferences_util.dart';
import 'package:hm_show_flutter/widgets/cached_image.dart';

class HomeMenuView extends StatelessWidget {
  final List<HomeDataAppmanulist> categoryList;

  HomeMenuView(this.categoryList);

  _goCategoryView(BuildContext context, HomeDataAppmanulist channel) {
    switch (channel.type) {
      case JumpType.GOODS_LIST:
        NavigatorUtils.goGoodsListPage(context);
        break;
      case JumpType.ACTIVITY_LIST:
        NavigatorUtils.goActivityListPage(context);
        break;
      case JumpType.RECOMMEND_LIST:
        NavigatorUtils.goCompanyListPage(context,CompanyType.RECOMMEND_LIST);
        break;
      case JumpType.BRAND_LIST:
        NavigatorUtils.goCompanyListPage(context,CompanyType.BRAND_LIST);
        break;
      case JumpType.STORE_INFO:
        SharedPreferencesUtils.getCompany().then((company) {
          if (company != null) {
            NavigatorUtils.goCompanyDetailPage(context, company.id);
          }
        });
        break;
      case JumpType.WEB_URL:
        NavigatorUtils.goWebView(context,null,channel.url);
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: GridView.builder(
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      itemCount: categoryList.length,
      itemBuilder: (BuildContext context, int index) {
        //  return _getGridViewItem(categoryList[index]);
        return _getGridViewItem(context, categoryList[index]);
      },
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        //单个子Widget的水平最大宽度
        crossAxisCount: 5,
        //水平单个子Widget之间间距
        mainAxisSpacing: ScreenUtil.instance.setWidth(10.0),
        //垂直单个子Widget之间间距
        crossAxisSpacing: ScreenUtil.instance.setWidth(10.0),
      ),
    ));
  }

  Widget _getGridViewItem(BuildContext context, HomeDataAppmanulist channel) {
    return Center(
      child: InkWell(
        onTap: () => _goCategoryView(context, channel),
        child: Column(
          children: <Widget>[
            CachedImageView(ScreenUtil.instance.setWidth(60.0),
                ScreenUtil.instance.setWidth(60.0), channel.url),
            Padding(
              padding: EdgeInsets.only(top: ScreenUtil.instance.setWidth(10.0)),
            ),
            Text(
              channel.title,
              style: TextStyle(
                  fontSize: ScreenUtil.instance.setSp(26.0),
                  color: Colors.black87),
            )
          ],
        ),
      ),
    );
  }
}
